﻿using ServiceStack.WebHost.Endpoints;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Pacs.Services
{
    public class AppHost : AppHostBase
    {
        public AppHost()//Base constructor requires a name and assembly to locate web service classes. 
            : base("ECG")
        {
            
        }
        protected override ServiceStack.ServiceHost.ServiceManager CreateServiceManager(params System.Reflection.Assembly[] assembliesWithServices)
        {
            assembliesWithServices = ServiceLoader.Find("bin").ToArray();
            return base.CreateServiceManager(assembliesWithServices);
        }
        /// <summary>
        /// 配置
        /// </summary>
        /// <param name="container"></param>
        public override void Configure(Funq.Container container)
        {
            ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;
            ServiceStack.Text.JsConfig.IncludeNullValues = true;
            ServiceStack.WebHost.Endpoints.EndpointHost.Config.ShowOprations = true;
            ServiceStack.WebHost.Endpoints.EndpointHost.Config.DebugMode = true;
        }
    }
    public class ServiceLoader
    {
        public static List<System.Reflection.Assembly> Find(string path)
        {
            var dllPath = AppDomain.CurrentDomain.BaseDirectory + path;

            var files = System.IO.Directory.GetFiles(dllPath);

            var assemblies = new List<System.Reflection.Assembly>();

            foreach (var file in files)
            {
                if (file.IndexOf(".Server.dll") > -1)
                {
                    assemblies.Add(System.Reflection.Assembly.LoadFile(file));
                }
            }
            return assemblies;
        }
    }
}